import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views//Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/Users'
import Rights from '../views/Power/Rights.vue'
import Roles from '../views/Power/Roles.vue'
import Cate from '../views/Goods/Cate.vue'
import Params from '../views/Goods/Params.vue'
import List from '../views/Goods/List.vue'
import Add from '../views/Goods/add.vue'
import Order from '../views/Order/Order.vue'
import Report from '../views/Report/Report.vue'

Vue.use(VueRouter)

// 缓存原型上的push函数
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace
// 给原型对象上的push指定新函数函数
VueRouter.prototype.push = function (location, onComplete, onAbort) {
    // 判断如果没有指定回调函数, 通过call调用源函数并使用catch来处理错误
    if (onComplete === undefined && onAbort === undefined) {
        return originPush.call(this, location, onComplete, onAbort).catch(() => { })
    } else { // 如果有指定任意回调函数, 通过call调用源push函数处理
        originPush.call(this, location, onComplete, onAbort)
    }
}
// replace同理处理
VueRouter.prototype.replace = function (location, onComplete, onAbort) {
    if (onComplete === undefined && onAbort === undefined) {
        return originReplace.call(this, location, onComplete, onAbort).catch(() => { })
    } else {
        originReplace.call(this, location, onComplete, onAbort)
    }
}
const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                component: Welcome
            },
        ]
    },
    {
        path: '/husers',
        component: Home,
        name: 'Husers',
        redirect: '/husers/users',
        meta: { title: '用户管理' },
        children: [
            {
                path: '/husers/users',
                component: Users,
                meta: { title: '用户列表' }
            },
        ]
    },
    {
        path: '/hrights',
        name: 'Hrights',
        component: Home,
        redirect: '/hrights/rights',
        meta: { title: '权限管理' },
        children: [
            {
                path: '/hrights/rights',
                component: Rights,
                meta: { title: '权限列表' }
            },
            {
                path: '/hrights/roles',
                component: Roles,
                meta: { title: '角色列表' }
            },
        ]
    },
    {
        path: '/hgoods',
        name: 'Hgoods',
        component: Home,
        redirect: '/hgoods/goods',
        meta: { title: '商品管理' },
        children: [
            {
                path: '/hgoods/goods',
                component: List,
                meta: { title: '商品列表' }
            },
            {
                path: '/hgoods/categories',
                component: Cate,
                meta: { title: '商品分类' }

            },
            {
                path: '/hgoods/params',
                component: Params,
                meta: { title: '分类参数' }

            },
            {
                path: '/hgoods/goods/add',
                component: Add,
                meta: { title: '添加商品' }
            }

        ]
    },
    {
        path: '/horders',
        name: 'Horder',
        component: Home,
        redirect: '/horders/orders',
        meta: { title: '订单管理' },
        children: [
            {
                path: '/horders/orders',
                component: Order,
                meta: { title: '订单列表' }
            }
        ]
    },
    {
        path: '/hreports',
        name: 'Hreport',
        component: Home,
        redirect: '/hreports/reports',
        meta: { title: '数据统计' },
        children: [
            {
                path: '/hreports/reports',
                component: Report,
                meta: { title: '数据报表' }
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login')
        next()
    const token = window.sessionStorage.getItem('token')
    if (!token) {
        next('/login')
    } else {
        next()
    }
})

export default router